package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.form.MessageQueryForm;
import com.springboot.cloud.demos.entity.param.UserQueryParam;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.producer.dao.MessageMapper;
import com.springboot.cloud.demos.entity.param.MessageQueryParam;
import com.springboot.cloud.demos.entity.po.Message;
import com.springboot.cloud.demos.producer.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Value("${producer.message:123}")
    private String value;

    @Override
    @CacheEvict(value = "message", allEntries = true)
    public boolean add(Message message) {
        return this.save(message);
    }

    @Override
    @CacheEvict(value = "message", allEntries = true)
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public IPage list(MessageQueryForm messageQueryForm) {
        MessageQueryParam messageQueryParam = messageQueryForm.toParam(MessageQueryParam.class);
        QueryWrapper<Message> queryWrapper = messageQueryParam.build();
        if (messageQueryForm.getType()!=null) {
            queryWrapper.eq("type", messageQueryForm.getType());
            queryWrapper.eq("to_user_id", messageQueryForm.getToUserId());
        }
        Page<Message> page = new Page<>(messageQueryForm.getCurrent(), messageQueryForm.getSize());
        queryWrapper.orderByDesc("a.created_time");
        return this.baseMapper.selectMapsPage(page, queryWrapper);
    }

    @Override
    @Cacheable(value = "message", key = "#root.targetClass+'-'+#messageQueryForm.toUserId+'-'+#messageQueryForm.type")
    public Integer count(MessageQueryForm messageQueryForm) {
        MessageQueryParam messageQueryParam = messageQueryForm.toParam(MessageQueryParam.class);
        QueryWrapper<Message> queryWrapper = messageQueryParam.build();
        if (messageQueryForm.getType()!=null) {
            queryWrapper.eq("type", messageQueryForm.getType());
            queryWrapper.eq("to_user_id", messageQueryForm.getToUserId());
            queryWrapper.eq("is_read", 0);
        }
        return this.count(queryWrapper);
    }

    @Override
    @CacheEvict(value = "message", allEntries = true)
    public boolean update(Message message) {
        return this.updateById(message);
    }

    @Override
    @Cacheable(value = "message", key = "#root.targetClass+'-'+#id")
    public Message get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List<Message> query(MessageQueryParam messageQueryParam) {
        QueryWrapper<Message> queryWrapper = messageQueryParam.build();
        if (StringUtils.isNotEmpty(messageQueryParam.getType())) {
            queryWrapper.eq("type", messageQueryParam.getType());
        }
        queryWrapper.orderByDesc("created_time");
        return this.list(queryWrapper);
    }
}
